<template>
  <div class="chain-sup">
    <div class="top flex-left">
      <div class="wallet-select flex-left">
        <label>钱包</label>
        <el-select
          v-model="searchForm.platform_id"
          placeholder="请选择"
          size="medium"
          @change="search()"
        >
          <el-option
            v-for="(item, index) in list"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <el-button type="primary" size="medium" @click="showDialog = true"
        >新增支持主链</el-button
      >
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        size="medium"
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column
          align="center"
          label="主链"
          prop="chain"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="币种名称"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="platform"
          label="平台"
        ></el-table-column>
        <el-table-column align="center" label="图标">
          <template slot-scope="item">
            <img :src="item.row.icon" class="scaleImg" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="item">
            <el-button type="text" style="color: red" @click="del(item.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pages
        :pages="searchForm"
        :total="total"
        @pageChange="pageChange"
        layout="prev, pager, next"
      ></pages>
      <!-- 增加、编辑窗口 -->
      <chain-dialog
        v-if="showDialog"
        :platform_id="searchForm.platform_id"
        @cancel="showDialog = false"
        @success="success()"
      ></chain-dialog>
    </div>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import ChainDialog from "@/components/WalletManage/AddParallelSup.vue";
import { getSupParallel, delSupParallel } from "./Wallet.service.js";
export default {
  components: {
    Pages,
    ChainDialog,
  },
  data() {
    return {
      loading: false,
      tableData: [],
      showDialog: false,
      total: 0,
      searchForm: {
        page: 1,
        size: 10,
        platform_id: String(this.$store.state.userInfo.platform_id),
      },
    };
  },
  computed: {
    list() {
      return this.$store.state.wallet_list;
    },
  },
  mounted() {
    this.search();
  },
  methods: {
    async search() {
      this.loading = true;
      const res = await getSupParallel(this.searchForm);
      this.tableData = res.data.items || [];
      this.total = res.data.total;
      this.loading = false;
    },
    del(item) {
      this.$confirm(
        `是否删除 <span class="confirm-tip-color">${item.chain}</span> 链?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          dangerouslyUseHTMLString: true,
        }
      )
        .then(() => {
          delSupParallel({ id: item.id }).then((res) => {
            if (res.code === 0)
              this.$notify.success({
                title: "提示",
                message: "删除成功",
                duration: 2000,
              });
            if (this.tableData.length <= 1)
              this.searchForm.page =
                this.searchForm.page - 1 > 0 ? this.searchForm.page - 1 : 1;
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    success() {
      this.showDialog = false;
      this.search();
    },
    pageChange(val) {
      this.searchForm.page = val;
      this.search(this.searchForm);
    },
  },
};
</script>

<style lang="scss">
.chain-sup {
  position: relative;
  min-height: calc(100vh - 100px);
  .top {
    .wallet-select {
      width: 230px;
      margin-right: 30px;
      color: #606266;
      label {
        width: 60px;
        flex-shrink: 1;
      }
    }
    .input-with-select {
      width: 241px;
      margin-right: 20px;
      .el-input-group__append {
        background-color: #409eff;
        color: #fff;
      }
    }
  }
  .data-table {
    position: relative;
    padding-bottom: 20px;
    background: #fff;
    border-radius: 5px;
    margin-top: 20px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .scaleImg {
      max-width: 120px;
      max-height: 23px;
    }
    .link {
      color: #409eff !important;
    }
  }
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
}
</style>